package com.minxi.web.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JspServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            String userName = req.getParameter("user_name");
            String user_pwd = req.getParameter("user_pwd");
            Class.forName("com.mysql.cj.jdbc.driver");
            String url = "jdbc:mysql://127.0.0.1:3306/student?serverTimeZone=Asia/Shanghai";
            String user = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, user, password);
            String sql = "select * from user_info";
            ResultSet resultSet = preparedStatement.executeQuery(sql);
            if (resultSet.next()){
                if ( req.getParameter("user_name").equals(userName)&& req.getParameter("user_pwd").equals(user_pwd)){
                    resp.sendRedirect(req.getContextPath()+"/index2.jsp");
                }else {
                    resp.sendRedirect(req.getContextPath()+"/index2.jsp");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                connection.close();
                preparedStatement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            String userName = req.getParameter("user_name");
            String id = req.getParameter("id");
            String userAge = req.getParameter("user_age");
            Class.forName("com.mysql.cj.jdbc.driver");
            String url = "jdbc:mysql://127.0.0.1:3306/student?serverTimeZone=Asia/Shanghai";
            String user = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, user, password);
            String sql = "insert into user_info(id,user_name,user_age) values(?,?,?,?);";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,Integer.parseInt(id));
            preparedStatement.setString(2,userName);
            preparedStatement.setInt(3,Integer.parseInt(userAge));
            int i = preparedStatement.executeUpdate(sql);
            if (i>0){
                System.out.println("新增成功");
            }else {
                System.out.println("新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {if (null!=connection){
                connection.close();
            }if (null!=preparedStatement){
                preparedStatement.close();
            }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }
}
